home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / rayshade / libray / libobj / list.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  3KB  |  153 lines

  1. /*
  2.  * list.c
  3.  *
  4.  * Copyright (C) 1989, 1991, Craig E. Kolb
  5.  * All rights reserved.
  6.  *
  7.  * This software may be freely copied, modified, and redistributed
  8.  * provided that this copyright notice is preserved on all copies.
  9.  *
  10.  * You may not distribute this software, in whole or in part, as part of
  11.  * any commercial product without the express consent of the authors.
  12.  *
  13.  * There is no warranty or other guarantee of fitness of this software
  14.  * for any purpose.  It is provided solely "as is".
  15.  *
  16.  * $Id: list.c,v 1.1 1994/05/14 19:50:54 millard Exp millard $
  17.  *
  18.  * $Log: list.c,v $
  19.  * Revision 1.1  1994/05/14  19:50:54  millard
  20.  * Initial revision
  21.  *
  22.  * Revision 4.0  91/07/17  14:38:42  kolb
  23.  * Initial version.
  24.  * 
  25.  */
  26. #include "geom.h"
  27. #include "list.h"
  28.  
  29. static Methods *iListMethods = NULL;
  30. static char listName[] = "list";
  31.  
  32. List *
  33. ListCreate()
  34. {
  35.     return (List *)share_calloc(1, sizeof(List));
  36. }
  37.  
  38. char *
  39. ListName()
  40. {
  41.     return listName;
  42. }
  43.  
  44. /*
  45.  * Take a list whose DATA field points to a linked list of objects and
  46.  * turn it into a List.
  47.  */
  48. int
  49. ListConvert(list, objlist)
  50. List *list;
  51. Geom *objlist;
  52. {
  53.     int num;
  54.  
  55.     /*
  56.      * Find the unbounded objects on the list as well as the
  57.      * bounding box of the list.
  58.      */
  59.     list->list = objlist;
  60.     for (num = 0; objlist; objlist = objlist->next)
  61.         num += objlist->prims;
  62.     return num;
  63. }
  64.  
  65. /*
  66.  * Intersect ray & list of objects.
  67.  */
  68. int
  69. ListIntersect(list, ray, hitlist, mindist, maxdist)
  70. List *list;
  71. Ray *ray;
  72. HitList *hitlist;
  73. Float mindist, *maxdist;
  74. {
  75.     Geom *objlist;
  76.     Vector vtmp;
  77.     Float s;
  78.     int hit;
  79.  
  80.     hit = FALSE;
  81.     /*
  82.      * Intersect with unbounded objects.
  83.      */
  84.     for (objlist = list->unbounded; objlist ; objlist = objlist->next) {
  85.         if (intersect(objlist, ray, hitlist, mindist, maxdist))
  86.             hit = TRUE;
  87.     }
  88.  
  89.     /*
  90.      * Check for intersection with bounding box.
  91.      */
  92.     s = *maxdist;    /* So maxdist won't be reset. */
  93.     VecAddScaled(ray->pos, mindist, ray->dir, &vtmp);
  94.     if (OutOfBounds(&vtmp, list->bounds) &&
  95.         !BoundsIntersect(ray, list->bounds, mindist, &s))
  96.         /*
  97.          * Ray never hit list.
  98.          */
  99.         return hit;
  100.     /*
  101.      * Else the ray enters list-space before it hits an
  102.      * unbounded object. Intersect with objects on list.
  103.      */
  104.     for (objlist = list->list; objlist ; objlist = objlist->next) {
  105.         if (intersect(objlist, ray, hitlist, mindist, maxdist))
  106.             hit = TRUE;
  107.     }
  108.  
  109.     return hit;
  110. }
  111.  
  112. Methods *
  113. ListMethods()
  114. {
  115.     if (iListMethods == (Methods *)NULL) {
  116.         iListMethods = MethodsCreate();
  117.         iListMethods->methods = ListMethods;
  118.         iListMethods->create = (GeomCreateFunc *)ListCreate;
  119.         iListMethods->name = ListName;
  120.         iListMethods->intersect = ListIntersect;
  121.         iListMethods->bounds = ListBounds;
  122.         iListMethods->convert = ListConvert;
  123.         iListMethods->checkbounds = FALSE;
  124.         iListMethods->closed = TRUE;
  125.     }
  126.     return iListMethods;
  127. }
  128.  
  129. void
  130. ListBounds(list, bounds)
  131. List *list;
  132. Float bounds[2][3];
  133. {
  134.     Geom *obj, *next;
  135.  
  136.     BoundsInit(list->bounds);
  137.     /*
  138.      * For each object on the list,
  139.      * compute its bounds...
  140.      */
  141.     list->unbounded  = GeomComputeAggregateBounds(&list->list, 
  142.                 list->unbounded, list->bounds);
  143.     BoundsCopy(list->bounds, bounds);
  144. }
  145.  
  146. void
  147. ListMethodRegister(meth)
  148. UserMethodType meth;
  149. {
  150.     if (iListMethods)
  151.         iListMethods->user = meth;
  152. }
  153.